// 微信配置模块
class WechatConfig {
  constructor() {
    this.config = {
      appid: process.env.WECHAT_APPID || "wx1252cf1f26c7faee",
      secret: process.env.WECHAT_SECRET || "127a12fe490f3d9852f96e2cecea1888",
      baseUrl: "https://api.weixin.qq.com",
      timeout: 10000, // 10秒超时
    };
  }

  // 获取配置
  getConfig() {
    return this.config;
  }

  // 检查配置是否完整
  checkConfig() {
    const { appid, secret } = this.config;

    if (
      !appid ||
      appid === "YOUR_APPID_HERE" ||
      appid === "wx1252cf1f26c7faee"
    ) {
      console.warn(
        "⚠️  警告: 未配置微信公众号appid，请设置 WECHAT_APPID 环境变量"
      );
    }

    if (
      !secret ||
      secret === "YOUR_APPSECRET_HERE" ||
      secret === "127a12fe490f3d9852f96e2cecea1888"
    ) {
      console.warn(
        "⚠️  警告: 未配置微信公众号appsecret，请设置 WECHAT_SECRET 环境变量"
      );
    }

    if (
      appid &&
      appid !== "YOUR_APPID_HERE" &&
      appid !== "wx1252cf1f26c7faee" &&
      secret &&
      secret !== "YOUR_APPSECRET_HERE" &&
      secret !== "127a12fe490f3d9852f96e2cecea1888"
    ) {
      console.log("✅ 微信配置检查通过");
    }
  }

  // 获取授权URL
  getAuthUrl(redirectUri, scope = "snsapi_base", state = "wechat_auth") {
    const { appid } = this.config;
    const encodedRedirectUri = encodeURIComponent(redirectUri);

    return `${this.config.baseUrl}/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodedRedirectUri}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`;
  }

  // 获取access_token URL
  getAccessTokenUrl(code) {
    const { appid, secret } = this.config;
    return `${
      this.config.baseUrl
    }/sns/oauth2/access_token?appid=${appid}&secret=${secret}&code=${encodeURIComponent(
      code
    )}&grant_type=authorization_code`;
  }

  // 获取用户信息URL
  getUserInfoUrl(accessToken, openid) {
    return `${
      this.config.baseUrl
    }/sns/userinfo?access_token=${encodeURIComponent(
      accessToken
    )}&openid=${encodeURIComponent(openid)}&lang=zh_CN`;
  }

  // 验证token URL
  getCheckTokenUrl(accessToken, openid) {
    return `${this.config.baseUrl}/sns/auth?access_token=${encodeURIComponent(
      accessToken
    )}&openid=${encodeURIComponent(openid)}`;
  }

  // 错误码映射
  getErrorMessage(errcode) {
    const errorMap = {
      40001: "access_token过期或无效",
      40003: "不合法的openid",
      40013: "不合法的appid",
      40029: "不合法的code",
      40125: "不合法的appsecret",
      45011: "API调用太频繁，请稍后再试",
      41008: "缺少code参数",
      42001: "access_token超时",
      42002: "refresh_token超时",
      43001: "需要GET请求",
      43002: "需要POST请求",
      43003: "需要HTTPS请求",
      44001: "多媒体文件为空",
      45001: "多媒体文件大小超过限制",
      45002: "消息内容超过限制",
      45003: "标题字段超过限制",
      45004: "描述字段超过限制",
      45005: "链接字段超过限制",
      45006: "图片链接字段超过限制",
      45007: "语音播放时间超过限制",
      45008: "图文消息超过限制",
      45009: "接口调用超过限制",
      45015: "回复时间超过限制",
      45016: "系统分组，不允许修改",
      45017: "分组名字过长",
      45018: "分组数量超过上限",
      46001: "不存在媒体数据",
      46002: "不存在的菜单版本",
      46003: "不存在的菜单数据",
      46004: "不存在的用户",
      47001: "解析JSON/XML内容错误",
      48001: "api功能未授权",
      50001: "用户未授权该api",
      61451: "参数错误",
      61452: "无效客服账号",
      61453: "客服帐号已存在",
      61454: "客服帐号名长度超过限制",
      61455: "客服帐号名包含非法字符",
      61456: "客服帐号数量超过限制",
      61457: "无效头像文件类型",
      61450: "系统错误",
      61500: "日期格式错误",
    };

    return errorMap[errcode] || "未知错误";
  }
}

module.exports = new WechatConfig();
